package cn.iocoder.yudao.module.dr.service.publish;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.dr.controller.admin.publish.vo.PublishPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.publish.vo.PublishSaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.publish.PublishDO;
import cn.iocoder.yudao.module.dr.dal.mysql.publish.PublishMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.PUBLISH_NOT_EXISTS;

/**
 *  出版申请 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class PublishServiceImpl implements PublishService {

    @Resource
    private PublishMapper publishMapper;

    @Override
    public Long createPublish(PublishSaveReqVO createReqVO) {
        // 插入
        PublishDO publish = BeanUtils.toBean(createReqVO, PublishDO.class);
        publishMapper.insert(publish);
        // 返回
        return publish.getId();
    }

    @Override
    public void updatePublish(PublishSaveReqVO updateReqVO) {
        // 校验存在
        validatePublishExists(updateReqVO.getId());
        // 更新
        PublishDO updateObj = BeanUtils.toBean(updateReqVO, PublishDO.class);
        publishMapper.updateById(updateObj);
    }

    @Override
    public void deletePublish(Long id) {
        // 校验存在
        validatePublishExists(id);
        // 删除
        publishMapper.deleteById(id);
    }

    private void validatePublishExists(Long id) {
        if (publishMapper.selectById(id) == null) {
            throw exception(PUBLISH_NOT_EXISTS);
        }
    }

    @Override
    public PublishDO getPublish(Long id) {
        return publishMapper.selectById(id);
    }

    @Override
    public PageResult<PublishDO> getPublishPage(PublishPageReqVO pageReqVO) {
        return publishMapper.selectPage(pageReqVO);
    }

}